/**
 * @description 登录、获取用户信息、退出登录、清除token逻辑，不建议修改
 */
import { useAclStore } from './acl'
import { useActivityStore } from './activity'
import { useSettingsStore } from './settings'
import { getUserInfo, login } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/token'
import { defineStore } from 'pinia'
import { ElNotification } from 'element-plus'
export const useUserStore = defineStore('user', {
  state: (): any => ({
    token: getToken() as string,
    username: '游客',
    avatar: '',
    userInfo: {},
  }),
  getters: {
    getToken: (state) => state.token,
    getUsername: (state) => state.username,
    getAvatar: (state) => state.avatar,
    getUserId: (state) => state.userInfo.id,
    getStoreUserInfo: (state) => state.userInfo,

    getActivityId: (state) => {
      return useActivityStore().getActivityId
    },
  },
  actions: {
    /**
     * @description 设置token
     * @param {*} token
     */
    setToken(token: string) {
      this.token = token
      setToken(token)
    },
    /**
     * @description 设置用户名
     * @param {*} username
     */
    setUsername(username: string) {
      this.username = username
    },
    /**
     * @description 设置头像
     * @param {*} avatar
     */
    setAvatar(avatar: string) {
      this.avatar = avatar
    },

    /**
     * @description 登录拦截放行时，设置虚拟角色
     */
    setVirtualRoles() {
      const aclStore = useAclStore()
      aclStore.setFull(true)
      this.setUsername('admin(未开启登录拦截)')
      this.setAvatar('https://i.gtimg.cn/club/item/face/img/2/15922_100.gif')
    },
    /**
     * @description 设置token并发送提醒
     * @param {string} token 更新令牌
     * @param {string} tokenName 令牌名称
     */
    afterLogin(token: string, tokenName: string) {
      const settingsStore = useSettingsStore()
      if (token) {
        this.setToken(token)
      } else {
        const err = `登录接口异常，未正确返回${tokenName}...`
        ElNotification({
          title: '提示',
          message: err,
          type: 'warning',
        })
        throw err
      }
    },
    /**
     * @description 登录
     * @param {*} userInfo
     */
    async login(userInfo: any) {
      const { code, message, result } = await login(userInfo)
      if (code == 0) {
        ElNotification({
          title: '提示',
          message: '登录成功',
          type: 'success',
        })
        this.afterLogin(result.token, 'token')
        return true
      } else {
        ElNotification({
          title: '提示',
          message: message,
          type: 'warning',
        })
        return false
      }
    },
    /**
     * @description 获取用户信息接口 这个接口非常非常重要，如果没有明确底层前逻辑禁止修改此方法，错误的修改可能造成整个框架无法正常使用
     * @returns
     */
    async getUserInfo() {
      const { result, code } = await getUserInfo()
      if (code == 0) {
        // 如不使用username用户名,可删除以下代码
        const aclStore = useAclStore()
        if (result) this.setUserInfo(result)
        if (result.avatar) this.setAvatar(result.avatar)
        if (result.username) this.setUsername(result.username)
        //@ts-ignore
        if (result.isSuper) {
          aclStore.setRole(['Admin'])
          aclStore.setFull(true)
        } else {
          aclStore.setRole([''])
          aclStore.setFull(false)
        }
      } else {
        return
      }
    },
    setUserInfo(userInfo: any) {
      this.userInfo = userInfo
    },
    /**
     * @description 退出登录
     */
    async logout() {
      this.token = ''
      removeToken()
    },
    /**
     * @description 重置token、roles、permission、router、tabsBar等
     */
    async resetAll() {
      this.token = ''
      removeToken()


      // localStorage.removeItem('lastDialogDate')
    },
  },
})
